//
//  PhonePickerViewController.swift
//  Kool Car
//
//  Created by huang weiqiang on 2020/5/21.
//  Copyright © 2020 鲜恬科技. All rights reserved.
//

import UIKit

typealias SelectPhoneAreaBlock = (_ code: String) -> ()

class PhoneAreaPickerVC: UIViewController {
    var callBack: SelectPhoneAreaBlock!
    var contentView: UIView!
    var pickerView: UIPickerView!
    var dataArray:[MobileAreaCodeModel] = []
    var bkgBtn:UIButton!
    var toolBarView: UIView!
    var contentH = 293 as CGFloat
    var currentIndex: NSInteger = 0
    var defaultCode: String = "86"
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.clear
        //背景按钮
        bkgBtn = UIButton.init(type: UIButtonType.custom)
        bkgBtn.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
        bkgBtn.backgroundColor =  UIColor.init(red: 0, green: 0, blue: 0, alpha: 0)
        self.view .addSubview(bkgBtn)
        bkgBtn.addTarget(self, action: #selector(hiddenViewAction), for: UIControlEvents.touchUpInside)
        
        //内容视图
        contentView = UIView.init(frame: CGRect.init(x: 0, y: kScreenHeight, width: kScreenWidth , height: contentH ))
        contentView.backgroundColor = UIColor.white
        bkgBtn.addSubview(contentView)
        
        //上边工具栏
        self.toolBarView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 44))
        self.toolBarView.backgroundColor = UIColor.white
        contentView.addSubview(self.toolBarView)
     
        // 取消按钮
        let cancelBtn = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 50, height: 44))
        cancelBtn.backgroundColor = UIColor.clear
        cancelBtn.setTitle(MMLocalizedString("cancel", comment: "取消"), for: UIControlState.normal)
        cancelBtn.setTitle(MMLocalizedString("cancel", comment: "取消"), for: UIControlState.highlighted)
        cancelBtn.setTitleColor(Theme.mainColor, for: UIControlState.normal)
        cancelBtn.setTitleColor(Theme.mainColor, for: UIControlState.highlighted)
        cancelBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 16)
        cancelBtn.addTarget(self , action: #selector(hiddenViewAction), for: UIControlEvents.touchUpInside)
        self.toolBarView.addSubview(cancelBtn)
        
        /// 确认按钮
        let okBtn = UIButton.init(frame: CGRect.init(x: self.toolBarView.frame.size.width - 50, y: 0, width: 50, height: 50))
        okBtn.backgroundColor = UIColor.clear
        okBtn.setTitle(MMLocalizedString("finish", comment: "完成"), for: UIControlState.normal)
        okBtn.setTitle(MMLocalizedString("finish", comment: "完成"), for: UIControlState.highlighted)
        okBtn.setTitleColor(Theme.mainColor, for: UIControlState.normal)
        okBtn.setTitleColor(Theme.mainColor, for: UIControlState.highlighted)
        okBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 16)
        okBtn.addTarget(self , action: #selector(confirmBtnAction), for: UIControlEvents.touchUpInside)
        self.toolBarView.addSubview(okBtn)
        
        //线条
        let lineView2 = UIView.init(frame: CGRect.init(x: 0, y: self.toolBarView.frame.maxY-1, width: self.toolBarView.frame.width, height: 1))
        lineView2.backgroundColor = UIColor.init(hex: 0xE1E3E6)
        self.toolBarView.addSubview(lineView2)
        
        self.pickerView = UIPickerView.init(frame: CGRect.init(x: 0, y: self.toolBarView.frame.maxY, width: kScreenWidth, height: 215))
        self.pickerView.delegate = self
        self.pickerView.dataSource = self
        self.contentView.addSubview(self.pickerView)
        self.findDefaultIndex()  //去找到默认的行

    }
        
    //去找到默认的行
    func findDefaultIndex(){
        var findIndex = 0 as NSInteger
        for kkObj in self.dataArray.enumerated() {
            let itemDict = kkObj.element
            let tempCode = itemDict.areaCode
            if (tempCode == self.defaultCode) {
                findIndex = kkObj.offset
                break
            }
        }
        self.currentIndex = findIndex
        self.pickerView.selectRow(self.currentIndex, inComponent: 0, animated: true)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        startShowActionSheetAnimtion()
    }
    
    ///确定
    @objc func confirmBtnAction(){
        if (self.callBack != nil && self.currentIndex < self.dataArray.count) {
            let tempDict = self.dataArray[self.currentIndex]
            let titleStr = String(format: "%@", tempDict.areaCode ?? "")
            self.callBack(titleStr)
        }
        self.hiddenViewAction()
    }
    
    /// 开始动画
    func startShowActionSheetAnimtion() {
        self.contentView.frame.origin.y = kScreenHeight
        let tableViewOrginY = kScreenHeight - contentView.frame.size.height
        let opts = UIViewAnimationOptions.curveEaseInOut
        UIView.animate(withDuration: 0.2, delay: 0, options: opts, animations: {
            self.contentView.frame.origin.y = tableViewOrginY
            self.bkgBtn.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.3)
        }, completion: { _ in
            
        })
    }
    
    /// 隐藏
    @objc func hiddenViewAction(){
        let opts = UIViewAnimationOptions.curveEaseInOut
        UIView.animate(withDuration: 0.2, delay: 0, options: opts, animations: {
            self.bkgBtn.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0)
            self.contentView.frame.origin.y = kScreenHeight
        }, completion: { _ in
            self.dismiss(animated: false, completion: nil)
        })
    }

}
extension PhoneAreaPickerVC: UIPickerViewDelegate, UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return self.dataArray.count
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 50
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if (self.dataArray.count > row) {
            let tempDict = self.dataArray[row]
            let titleStr = String(format: "%@ %@", tempDict.country ?? "", tempDict.areaCode ?? "")
            return titleStr.replacingOccurrences(of: "+", with: "")
        }
        return ""
    }
    ///选中的行
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if (self.dataArray.count > row) {
            self.currentIndex = row
        }
    }
    
}
